<template>
    <div id="line-bar" ref="canvas" class="canvas" :style="{width: '100%', height: canHeight + 'px'}"></div>
</template>
<script setup lang="ts">
import { defineProps, computed, onMounted } from 'vue'
import { createPinia } from 'pinia'
import * as echarts from 'echarts/core';
import {
  ToolboxComponent,
  type ToolboxComponentOption,
  TooltipComponent,
  type TooltipComponentOption,
  GridComponent,
  type GridComponentOption,
  LegendComponent,
  type LegendComponentOption
} from 'echarts/components';
import {
  BarChart,
  type BarSeriesOption,
  LineChart,
  type LineSeriesOption
} from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import { useChartsStore } from '@/stores/charts';
const pinia = createPinia();
const chartStore = useChartsStore(pinia)
echarts.use([
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  LineChart,
  CanvasRenderer,
  UniversalTransition
]);

type EChartsOption = echarts.ComposeOption<
  | ToolboxComponentOption
  | TooltipComponentOption
  | GridComponentOption
  | LegendComponentOption
  | BarSeriesOption
  | LineSeriesOption
>;
const props = defineProps({
    canvasHeight: {
        type:Number,
        default: 300
    },
    data: {
        type: Object,
        default: () => { } 
    }
})
let canHeight = computed(() => { return props.canvasHeight })
onMounted(() => {
    var chartDom = document.getElementById('line-bar')!;
    var myChart = echarts.init(chartDom);
    var option: EChartsOption;

    option = {
        tooltip: {
            trigger: 'axis',
                axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
            }
        },
        toolbox: {
            show: false,
            feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
            }
        },
        grid: {
            top: 40,
            left: '4%',
            right: '2%',
            bottom: 0,
            containLabel: true
        },
        legend: {
            show: true,
            left: 'center',
            top: 10,
            textStyle: { color: '#11cb92' },
            data: ['白天', '晚上', '占比']
        },
        xAxis: [{
            type: 'category',
            axisLabel: { show: true, color: '#c3c6ce',
                formatter: function (value) { return chartStore.weekEnToCnSecond[value]; }
            },
            data: props.data.xAxisDataList,
            axisPointer: { type: 'shadow' },
            
        }],
        yAxis: [
            {
                type: 'value',
                min: 0,
                max: 250,
                interval: 50,
                axisLabel: { show: true, color: '#c3c6ce' },
                splitLine:{show: true, lineStyle:{color: '#19485d'}},
                axisLine: { show: true },
                axisTick: { show: true },
            },
            {
                show: false,
                type: 'value',
                name: '比率',
                min: 0,
                // max: 25,
                interval: 5,
                axisLabel: { formatter: '{value} °C' }
            }
        ],
        series: props.data.seriesDataList
    };

    option && myChart.setOption(option);
})
</script>

<style scope>
</style>
